package com.et.generator.impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

import com.et.generator.ControllerProducer;

/**
 * <p>控制器生产实现</p>
 * @author defier Lai
 * <p>Date: 2009-12-6 下午10:02:42</p>
 * @version 1.0
 */
public class ControllerProducerImpl implements ControllerProducer {

	private String packageName;
	private String className;
	private String webRoot = "WebRoot";
	
	@Override
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	
	@Override
	public void setClassName(String className) {
		this.className = className;
	}

	@Override
	public void execute() throws Exception {
		if(className == null || className.trim().equals("") || packageName == null || packageName.trim().equals("")) {
			throw new IllegalArgumentException("参数错误或者参数不全");
		}
		
		generatorApplicationController();
		
		String tmpClassName = className;
		className = String.valueOf(className.charAt(0)).toUpperCase() + className.substring(1) + "Controller";
		
		StringBuilder body = new StringBuilder();
		
		body.append("package " + packageName + ";\r\n");
		body.append("import com.et.mvc.View;" + "\r\n\r\n");
		body.append("/**" + "\r\n" 
					+ " * <p>" + className + "</p>" + "\r\n"
					+ " * @author et2mvc" + "\r\n" 
					+ " * @version 1.0" + "\r\n"
					+ " */" + "\r\n");
		body.append("public class " + className
					+ " extends  ApplicationController {" + "\r\n\r\n");
		
		body.append(createMethod());
		
		body.append("}");
		
		File path = new File(System.getProperty("user.dir") + "/src/" + packageName.replace('.', '/'));
		if (!path.exists()) {
			path.mkdirs();
		}
		
		File file = new File(path + "/" + className + ".java");
		if(!file.exists() && file.createNewFile()) {
			createFile(body.toString(), file);
			System.out.println(className + ".java生成成功...");
			generatorTemplet(tmpClassName);	
			System.out.println(className + "对应页面生成成功...");
			
		} else {
			System.err.println(className + ".java已经存在，执行失败...");
		}
		
		
	}

	/**
	 * 生成applicationController
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 */
	private void generatorApplicationController() throws FileNotFoundException, IOException {
		StringBuilder body = new StringBuilder();
		body.append("package " + packageName + ";\r\n");
		body.append("import com.et.mvc.easy.EasyController;" + "\r\n");
		body.append("/**" + "\r\n" 
					+ " * <p>所有控制器基类</p>" + "\r\n"
					+ " * @author et2mvc" + "\r\n" 
					+ " * @version 1.0" + "\r\n"
					+ " */" + "\r\n");
		body.append("public class ApplicationController extends EasyController {" + "\r\n\r\n\r\n\r\n\r\n\r\n\r\n");
		body.append("}");
		File path = new File(System.getProperty("user.dir") + "/src/" + packageName.replace('.', '/'));
		if (!path.exists()) {
			path.mkdirs();
		}
		File file = new File(path + "/ApplicationController.java");
		if(!file.exists() && file.createNewFile()) {
			createFile(body.toString(), file);
		}
	}

	private void createFile(String body, File file) throws FileNotFoundException, IOException {
		OutputStreamWriter os = new OutputStreamWriter(new FileOutputStream(file),Charset.forName("utf-8"));
		os.write(body);
		os.flush();
		os.close();
	}

	private void generatorTemplet(String tmpClassName) throws IOException {
		File file = null;
		File path = new File(System.getProperty("user.dir") + "/"+ webRoot +"/views/default/" + tmpClassName);
		if(!path.exists()) {
			path.mkdirs();
		}
		file = new File(path + "/index.ftl");
		if(!file.exists()) 
			file.createNewFile();
		
		file = new File(path + "/edit.ftl");
		if(!file.exists()) 
			file.createNewFile();
		
		file = new File(path + "/form.ftl");
		if(!file.exists()) 
			file.createNewFile();
		
		file = new File(path + "/preNew.ftl");
		if(!file.exists()) 
			file.createNewFile();
	}

	private String createMethod() {
		StringBuilder body = new StringBuilder();
		body.append("	public View index() { \r\n\r\n"+
		"		// 将需要的数据渲染在页面上  \r\n"+
		"		render(\"key\", \"value\");\r\n"+
		"		// et2mvc默认采用的是freemarker页面引擎 \r\n"+
		"		return getDefaultView(); \r\n"+ 
		"	}\r\n\r\n");
		
		body.append("	public View preNew() {\r\n\r\n"+
		"		return getDefaultView();\r\n"+ 
		"	} \r\n\r\n");
		
		body.append("	public void create() throws Exception {\r\n\r\n"+
				"		render(\"popMessage\", \"创建成功！\");\r\n"+ 
				"		redirect(\"index\");\r\n"+
				"	} \r\n\r\n");
		
		body.append("	public View edit(int id) {\r\n\r\n"+
				"		return getDefaultView();\r\n"+ 
				"	} \r\n\r\n");
		
		body.append("	public void update() throws Exception {\r\n\r\n"+
				"		render(\"popMessage\", \"更新成功！\");\r\n"+ 
				"		redirect(\"index\");\r\n"+
				"	} \r\n\r\n");
		
		body.append("	public void destory(int id) throws Exception {\r\n\r\n"+
				"		render(\"popMessage\", \"删除成功！\");\r\n"+ 
				"		redirect(\"index\");\r\n"+
				"	} \r\n\r\n");
		
		return body.toString();
	}

	@Override
	public void setWebRoot(String webRoot) {
		this.webRoot = webRoot;
	}

}
